﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Tasks
{
    [FlowAttributes(nameof(SystemTask), 1)]
    public class SystemTask : ITask
    {
        #region 定时器
        private Ton _tonStartDelay = new Ton();
        private Ton _tonBuzzer = new Ton();
        private Ton _tonRed = new Ton();
        private Ton _tonYellow = new Ton();
        private Ton _tonGreen = new Ton();
        #endregion
        #region 输入
        private NormalOpen _inputStart;
        #endregion
        #region 输出
        private ICoil _red;
        private ICoil _yellow;
        private ICoil _green;
        private ICoil _buzzer;
        private ICoil _X轴使能;
        private ICoil _Y轴使能;
        #endregion
        #region 变量
        private bool _buzzerNegation = false, _alarmNegation = false, _maintainNegation = false;
        #endregion
        #region 上升沿
        private bool _homeAlarm = false, _homeRisingEdge = false, _alarmStop = false;
        #endregion
        private AntMovement.Parameter.TemporaryParameter _temporary;
        private IDialogHost _dialogHost;
        public SystemTask(IDialogHost dialogHost, ITemporaryParameter temporary, AntMovement.Core.IParameter.IIOParameter iop, AntMovement.Parameter.AxisParameter axisp)
        {
            _dialogHost = dialogHost;
            _temporary = (Parameter.TemporaryParameter)temporary;
            _inputStart = iop.GetNormalOpen("启动");
            _red = iop.GetCoil("红灯");
            _yellow = iop.GetCoil("黄灯");
            _green = iop.GetCoil("绿灯");
            _buzzer = iop.GetCoil("蜂鸣器");
            _temporary.NG满料 = false;
            _temporary.OK满料 = false;
            _temporary.SystemStatus.Auto = false;
            _temporary.SystemStatus.ManualOperation = true;
        }

        public void Resetting()
        {
            if (!_temporary.SystemStatus.Auto) 
            {
                _temporary.SystemStatus.ManualOperation = true;
                _temporary.Status_Device = Core.Enum.StatusDeviceType.手动;
            }
            _temporary.SystemStatus.AlarmStop = false;
            _temporary.NG满料 = false;
            _temporary.OK满料 = false;
            _yellow.Execute(true);
            _green.Execute(false);
            if (_homeAlarm)
            {
                _homeAlarm = false;
                _homeRisingEdge = false;
            }
            _alarmStop = false;
        }

        public void Handle()
        {
            _temporary.SystemStatus.AlarmStop = _homeAlarm || _temporary.SystemStatus.AlarmStop;
            if (_inputStart.Execute())
            {
                /*
                 长按启动键三秒钟,蜂鸣器频响,切换自动状态
                 */
                if (!_temporary.SystemStatus.Auto && !_temporary.SystemStatus.AlarmStop && !_temporary.SystemStatus.Maintain)
                {
                    if (_temporary.SystemStatus.ManualOperation || _temporary.SystemStatus.MachineStop)
                    {
                        _tonStartDelay.Time(3000, true);
                        if (_tonStartDelay.Out)
                        {
                            if (!_homeRisingEdge && !_temporary.HomeComplete)
                            {
                                _dialogHost.DialogShow("轴没有全部回原，先回原在开机！！！", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
                                _homeAlarm = true;
                                _homeRisingEdge = true;
                            }
                            if (!_homeAlarm)
                            {
                                _temporary.Status_Device = Core.Enum.StatusDeviceType.自动运行;
                                _temporary.SystemStatus.ManualOperation = false;
                                _temporary.SystemStatus.MachineStop = false;
                                _temporary.SystemStatus.Auto = true;
                            }
                        }
                        else
                        {
                            _tonBuzzer.Time(500, !_tonBuzzer.Out);
                            if (_tonBuzzer.Out && !_buzzerNegation)
                            {
                                _green.Execute(true);
                                _buzzer.Execute(true);
                                _yellow.Execute(false);
                                _buzzerNegation = true;
                            }
                            else if (_tonBuzzer.Out && _buzzerNegation)
                            {
                                _green.Execute(false);
                                _buzzer.Execute(false);
                                _yellow.Execute(false);
                                _buzzerNegation = false;
                            }
                        }
                    }
                }
            }
            else
            {
                _tonStartDelay.Time(3000, false);
                if (_temporary.SystemStatus.ManualOperation && (!_temporary.NG满料 && !_temporary.OK满料))
                {
                    _temporary.Status_Device = Core.Enum.StatusDeviceType.手动;
                    _red.Execute(false);
                    _yellow.Execute(true);
                    _green.Execute(false);
                    _buzzer.Execute(false);
                }
            }
            _temporary.SystemStatus.ManualOperation = !_temporary.SystemStatus.Maintain && !_temporary.SystemStatus.Auto && !_temporary.SystemStatus.WaitMaterials 
                && !_temporary.SystemStatus.AlarmStop && !_temporary.SystemStatus.MachineStop;
            if (_temporary.SystemStatus.ManualOperation)
            {
                _temporary.Status_Device = Core.Enum.StatusDeviceType.手动;
            }
            if (_temporary.SystemStatus.AlarmStop)
            {
                _temporary.SystemStatus.MachineStop = false;
                _temporary.SystemStatus.ManualOperation = false;
                _temporary.SystemStatus.Maintain = false;
                _temporary.SystemStatus.Auto = false;
                _temporary.SystemStatus.WaitMaterials = false;
                _temporary.Status_Device = Core.Enum.StatusDeviceType.报警;
                _tonBuzzer.Time(300, !_tonBuzzer.Out);
                if (_tonBuzzer.Out && !_alarmNegation)
                {
                    _red.Execute(true);
                    _yellow.Execute(false);
                    _green.Execute(false);
                    _buzzer.Execute(true);
                    _alarmNegation = true;
                }
                else if (_tonBuzzer.Out && _alarmNegation)
                {
                    _red.Execute(false);
                    _yellow.Execute(false);
                    _green.Execute(false);
                    _buzzer.Execute(false);
                    _alarmNegation = false;
                }
            }
            else
            {
                if (_temporary.NG满料 || _temporary.OK满料)
                {
                    _red.Execute(false);
                    _yellow.Execute(true);
                    _green.Execute(false);
                    _buzzer.Execute(true);
                }
            }
            if (_temporary.SystemStatus.Auto && !_temporary.NG满料 && !_temporary.OK满料)
            {
                _red.Execute(false);
                _yellow.Execute(false);
                _green.Execute(true);
                _buzzer.Execute(false);
            }
            if (_temporary.SystemStatus.Maintain)
            {
                _tonYellow.Time(500, !_tonYellow.Out);
                if (_tonYellow.Out && !_maintainNegation)
                {
                    _red.Execute(false);
                    _yellow.Execute(false);
                    _green.Execute(false);
                    _buzzer.Execute(false);
                }
                else if (_tonYellow.Out && _maintainNegation)
                {
                    _red.Execute(false);
                    _yellow.Execute(true);
                    _green.Execute(false);
                    _buzzer.Execute(false);
                }
            }
        }

        public void LightWeight()
        {

        }

        public void SafeStop()
        {
            _temporary.SystemStatus.Auto = false;
        }

        public void EmergencyStop()
        {
            if (!_alarmStop)
            {
                _alarmStop = true;
                _dialogHost.DialogShow("急停按钮被按下！！！", Core.Enum.LogLevel.Error, hostDialog: Core.Enum.PopUpType.Global);
            }
            _temporary.SystemStatus.AlarmStop = true;
            _temporary.SystemStatus.Auto = false;
        }
    }
}
